package com.dhnsoft.education.controller.edu.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.edu.EduComment;
import com.dhnsoft.education.entity.edu.EduCommentReply;
import com.dhnsoft.education.entity.userCenter.UcenterMember;
import com.dhnsoft.education.service.edu.EduCommentReplyService;
import com.dhnsoft.education.service.edu.EduCommentService;
import com.dhnsoft.education.service.userCenter.UcenterMemberService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.R;
import com.dhnsoft.education.vo.UcenterMemberInfoCommonVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName CommentFrontController
 * @Author dhn
 * @date 2022.04.12 20:07
 */
@RestController
@RequestMapping("eduservice/commentFront")
//@CrossOrigin
public class CommentFrontController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private EduCommentReplyService replyService;

    @Autowired
    private UcenterMemberService memberService;

    //分页显示评论的方法
    @GetMapping("getCommentPage/{page}/{limit}/{courseId}")
    public R getCommentPage(@PathVariable Long page, @PathVariable Long limit, @PathVariable String courseId){
        Page<EduComment> pageParam = new Page<>(page,limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("gmt_create");
        commentService.page(pageParam,wrapper);
        List<EduComment> commentList = pageParam.getRecords();
        //查询该评论中的子评论
        for (int i = 0;i<commentList.size();i++) {
            QueryWrapper<EduCommentReply> replyWrapper = new QueryWrapper<>();
            replyWrapper.eq("comment_id",commentList.get(i).getId());
            List<EduCommentReply> replyList = replyService.list(replyWrapper);
            commentList.get(i).setCommentReplies(replyList);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    //添加评论到数据库
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request ){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            System.out.println("未登录!!!");
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberInfoCommonVo userInfo = getUserInfo(memberId);
        comment.setAvatar(userInfo.getAvatar());
        comment.setNickname(userInfo.getNickname());
        commentService.save(comment);
        return R.ok();
    }
    //添加子评论到数据库
    @PostMapping("addCommentReply")
    public R addCommentReply(@RequestBody EduCommentReply commentReply, HttpServletRequest request ){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录");
        }
        commentReply.setMemberId(memberId);
        UcenterMemberInfoCommonVo userInfo = getUserInfo(memberId);
        commentReply.setAvatar(userInfo.getAvatar());
        commentReply.setNickname(userInfo.getNickname());
        replyService.save(commentReply);
        return R.ok();
    }
    //实现点赞人数记录功能
    @PostMapping("likeNum")
    public R likeNum(@RequestBody EduComment eduComment){
        //根据传过来的comment的id查询评论
        //String id = eduComment.getId();
        //EduComment comment = commentService.getById(id);
        //comment.setLikeNum(eduComment.getLikeNum());
        commentService.updateById(eduComment);
        return R.ok();
    }

    public UcenterMemberInfoCommonVo getUserInfo(@PathVariable String id){
        //根据用户id来查询数据
        UcenterMember userInfo = memberService.getById(id);
        UcenterMemberInfoCommonVo infoCommonVo = new UcenterMemberInfoCommonVo();
        BeanUtils.copyProperties(userInfo,infoCommonVo);
        System.out.println(infoCommonVo);
        return infoCommonVo;
    }

    //查询该在线的用户id
    @GetMapping("queryUserId")
    public R queryUserId(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            System.out.println("未登录!!!");
            return R.error().code(28004).message("请登录");
        }
        return R.ok().data("memberId",memberId);
    }
}
